﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Cryptography;
using System.Globalization;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Facades;
using System.Web.Security;

public static class AMSManager
{
    public static bool ISAjaxRequest
    {
        get
        {
            return HttpContext.Current.Request.Headers["X-Requested-With"] != null && HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
        }
    }

    public static bool InsideIframe
    {
        get
        {
            return HttpContext.Current.Request.QueryString["iframe"] != null ? Convert.ToBoolean(HttpContext.Current.Request.QueryString["iframe"]) : false;
        }
    }

    

    #region Handle Dates
    public static void HandleCreateDates(object entity, ModelStateDictionary modelState)
    {
        List<string> datekeys = new List<string>();
        datekeys = modelState.Keys.Where(k => k.EndsWith("Date")).Select(x => x).ToList();
        foreach (string key in datekeys)
        {
            string formValue = HttpContext.Current.Request.Form[key];
            if (!string.IsNullOrEmpty(formValue))
            {
                DateTime? formatedDate = formValue.ToFormatedDate();
                if (formatedDate != null)
                {
                    modelState.Remove(key);
                    modelState.Add(key, new ModelState());
                    modelState.SetModelValue(key, new ValueProviderResult(formatedDate.Value.ToShortDateString(), formatedDate.Value.ToShortDateString(), null));

                    entity.GetType().GetProperty(key).SetValue(entity, formatedDate.Value, null);
                }
            }
        }
    }

    public static void HandleEditDates(object entity, FormCollection collection)
    {
        List<string> datekeys = new List<string>();
        datekeys = collection.AllKeys.Where(k => k.EndsWith("DateValue")).Select(x => x).ToList();
        foreach (string key in datekeys)
        {
            string formValue = HttpContext.Current.Request.Form[key];
            if (!string.IsNullOrEmpty(formValue))
            {
                DateTime? formatedDate = formValue.ToFormatedDate();
                if (formatedDate != null)
                {
                    entity.GetType().GetProperty(key.Substring(0, key.IndexOf("DateValue") + 4)).SetValue(entity, formatedDate.Value, null);
                }
            }
        }
    }
    #endregion // Handle Dates

    /// <summary>
    /// Returns an random interger number within a specified rage
    /// </summary>
    /// <param name="min">Minimum number</param>
    /// <param name="max">Maximum number</param>
    /// <returns>Result</returns>
    public static int GenerateRandomInteger(int min = 0, int max = 2147483647)
    {
        var randomNumberBuffer = new byte[10];
        new RNGCryptoServiceProvider().GetBytes(randomNumberBuffer);
        return new Random(BitConverter.ToInt32(randomNumberBuffer, 0)).Next(min, max);
    }

    public static int CompareCultures(CultureInfo x, CultureInfo y)
    {
        if (x == null)
        {
            if (y == null)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
        else
        {
            if (y == null)
            {
                return 1;
            }
            else
            {

                return x.IetfLanguageTag.CompareTo(y.IetfLanguageTag);
            }
        }
    }

}
